﻿using Unity.Burst;
using Unity.Collections;
using Unity.Mathematics;

namespace Game.FlowField.Burst
{
    [BurstCompile]
    public struct BrushHelper
    {
        private int m_BrushSize;
        private int2 m_Index2D;

        public BrushHelper(int brushSize, int2 index2D)
        {
            this.m_BrushSize = brushSize;
            this.m_Index2D = index2D;
        }

        [BurstCompile]
        public void CalculateCoverIndex2D(NativeArray<int2> result)
        {
            int2 minIndex2D = new int2(
                (int)math.ceil(this.m_Index2D.x - 0.5f * this.m_BrushSize), 
                (int)math.ceil(this.m_Index2D.y - 0.5f * this.m_BrushSize));

            var radius = m_BrushSize * 0.5f;
            var radiusSrq = radius * radius;
            var size = this.m_BrushSize + 1;
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    var newIndex2D = minIndex2D + new int2(i, j);
                    var indexDir = newIndex2D - this.m_Index2D;
                    var distanceSqr = indexDir.x * indexDir.x + indexDir.y * indexDir.y;

                    if (distanceSqr <= radiusSrq)
                    {
                        result[j + size * i] = newIndex2D;
                    }
                    else
                    {
                        result[j + size * i] = new int2(-1, -1);
                    }
                }
            }
        }
    }
}
